import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View, Button, Image, TouchableHighlight
} from 'react-native';
import DeviceRn from '../../components/Tools/device';
import Http from '../../components/Tools/http.unite';
import Toast, { DURATION } from 'react-native-easy-toast';
let DevWH = DeviceRn().getWidHig();
export default class MessageScreen extends Component {
    static navigationOptions = ({ navigation, screenProps }) => (
        {
            title: '消息提醒',
            headerStyle: {
                paddingLeft:4,
                backgroundColor: screenProps.header,
                borderBottomColor: '#eee',
                borderBottomWidth:1
            },
            headerTintColor: screenProps.color,
            headerTitleStyle: {
                color: screenProps.color
            },
        });
    constructor(props) {
        super(props);
        this.theme = this.props.screenProps;
        this.type = 3;
        this.state = {};
        this.skey = 'message_key';
        this.getHistory();
    }

    //获取历史记录
    getHistory() {
        let timer = new Date().getTime();
        DeviceRn().storageGet(this.skey).then(data => {
            if (data != null) {
                let datajson = JSON.parse(data);
               (timer - 900000)> datajson.time?this.setState({ data: datajson }):this.getData();           
            } else {
                this.getData();
            }
        }).catch(err => {
            this.refs.toast.show('获取数据失败');
        })
    }

    getData() {
        let payload = {
            type: this.type,
            i: 5
        };
        Http().httpPost('Interfaces/notice', payload).then(data => {
            let resPones = data;
            if (resPones.code == 1) {
                let paydata = {
                    data: resPones.data,
                    time: new Date().getTime()
                };
                this.setState({ data: resPones.data });
                DeviceRn().storageSave(this.skey, JSON.stringify(paydata));
            }
        }).catch(err => {
            this.refs.toast.show('网络异常');
        })
    }

    _rendItem(){
        return(
            <View style={styles.contain}>
                <Text>{this.state.data}</Text>
            </View>
        )
    }

    _rendEmpty(){
        return(
            <View style={[styles.contain,styles.empty]}>
               <Image style={{tintColor:this.theme.bgColor}} source={require('../../assets/images/no_data.png')}></Image>
               <Text style={styles.empty_txt}>一条消息都没有~</Text>
            </View>
        )
    }

    render() {
        return (
            <View style={styles.contain}>
                <Toast ref="toast" position="center" />
                {this.state.data?this._rendItem():this._rendEmpty()}
            </View>
        );
    }
};
const styles = StyleSheet.create({
    contain: {
        flex: 1,
        backgroundColor:'#fff',
    },
    empty:{
        justifyContent: 'flex-start',
        alignItems: 'center',
        marginTop:35
    },
    empty_txt:{
        color:'#888',
        marginTop:15
    },
});